package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Goal;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class GoalDao {
	private DBHelp<Goal> dbhelp = new DBHelp<Goal>();
	
	private class GoalRowMapper implements RowMapper<Goal> {

		public Goal mapperRow(ResultSet rs) throws SQLException {
			Goal goal = new Goal();
			
			goal.setId(rs.getString("id"));
			goal.setName(rs.getString("name"));
			goal.setDesc(rs.getString("desc"));
			goal.setCreatetime(rs.getString("createtime"));
			goal.setEmployeeid(rs.getString("employeeid"));
			goal.setProjectid(rs.getString("projectid"));
			goal.setRate(rs.getInt("rate"));
			return goal;
		}
		
	}

	public List<Goal> findAllById(String employeeid, String projectid) {
		String sql = "select * from t_goal where employeeid = ? and projectid = ? order by createtime desc";
		return dbhelp.executeQueryForList(sql, new GoalRowMapper(), employeeid,projectid);
	}

	public void save(Goal goal) {
		String sql = "insert into t_goal(id,`name`,`desc`,createtime,employeeid,projectid) values(?,?,?,?,?,?)";
		dbhelp.executeSQL(sql, goal.getId(),goal.getName(),goal.getDesc(),goal.getCreatetime(),goal.getEmployeeid(),goal.getProjectid());
	}

	public void update(String name, String desc, String id) {
		String sql = "update t_goal set `name` = ?,`desc` = ? where id = ?";
		dbhelp.executeSQL(sql, name,desc,id);
	}

	public Goal findById(String goalid) {
		String sql = "select * from t_goal where id = ?";
		return dbhelp.executeQueryForObject(sql, new GoalRowMapper(), goalid);
	}
	
	public void updateRate(String id, int goalRate) {
		String sql = "update t_goal set rate = ? where id = ?";
		dbhelp.executeSQL(sql, goalRate,id);
	}
}
